#include<iostream>
using namespace std;
#include<vector>
#include<queue>
class Solution {
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        int n = isConnected.size();
        vector<bool> check(n, false);
        queue<int> que;
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (check[i] == false) {
                que.push(i);
                check[i] = true;
                ++ans;
            }
            while (que.size()) {
                int now = que.front();
                for (int i = 0; i < n; ++i) {
                    if (isConnected[now][i] && check[i] == false) {
                        check[i] = true;
                        que.push(i);
                    }
                }
                que.pop();
            }
        }
        return ans;
    }
};